package com.film.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.film.dao.FilmInfoDao;
import com.film.exception.MyException;
import com.film.po.FilmInfo;
import com.film.service.FilmInfoService;

/**
 * 电影信息Service
 * @author Administrator
 *
 */
@Service
public class FilmInfoServiceImpl implements FilmInfoService {

	//电影信息dao
	private FilmInfoDao filmInfoDao;
	
	@Autowired
	public void setFilmInfoDao(FilmInfoDao filmInfoDao) {
		this.filmInfoDao = filmInfoDao;
	}

	@Transactional
	@Override
	public FilmInfo add(FilmInfo filmInfo) {
		
		int result = filmInfoDao.insertNonEmptyFilmInfo(filmInfo);
		
		if (result != 1) {
			throw new MyException("添加电影失败，电影的名称为：" + filmInfo.getFilmName());
		}

		FilmInfo filmInfo2 = filmInfoDao.selectFilmInfoById(filmInfo.getFilmId());

		if (filmInfo2 == null) {
			throw new MyException("查询电影失败，电影的id为：" + filmInfo.getFilmId());
		}

		return filmInfo2;
	}

	@Transactional
	@Override
	public void deleteById(int id) {
		int result = filmInfoDao.deleteFilmInfoById(id);
		if (result != 1) {
			throw new MyException("删除电影失败，电影的id为：" + id);
		}
	}

	@Transactional
	@Override
	public FilmInfo update(FilmInfo filmInfo) {
		FilmInfo filmInfo2 = filmInfoDao.selectFilmInfoById(filmInfo.getFilmId());

		if (filmInfo2 == null) {
			throw new MyException("要更新的电影不存在，电影的id为：" + filmInfo.getFilmId());
		}

		int result = filmInfoDao.updateNonEmptyFilmInfoById(filmInfo);

		if (result != 1) {
			throw new MyException("更新类型失败，类型的id为：" + filmInfo.getFilmId());
		}

		FilmInfo filmInfo3 = filmInfoDao.selectFilmInfoById(filmInfo.getFilmId());

		return filmInfo3;
	}

	@Override
	public FilmInfo findById(int id) {
		FilmInfo filmInfo = filmInfoDao.selectFilmInfoById(id);

		if (filmInfo == null) {
			throw new MyException("要查找的电影不存在，电影的id为：" + id);
		}
		return filmInfo;
	}

	@Override
	public List<FilmInfo> getList() {
		List<FilmInfo> list = filmInfoDao.selectFilmInfo();
		if(list==null || list.size()<1){
			throw new MyException("查找电影失败");
		}
		return list;
	}

	@Override
	public List<FilmInfo> getListByFilmTypeId(int typeId) {
		List<FilmInfo> list = filmInfoDao.selectFilmInfoByTypeId(typeId);
		if(list==null || list.size()<1){
			throw new MyException("根据电影类别查找电影失败,类别id:"+typeId);
		}
		return list;
	}

	@Override
	public List<FilmInfo> selectFilmInfoByParam(FilmInfo filmInfo) {
		List<FilmInfo> list = filmInfoDao.selectFilmInfoByParam(filmInfo);
		if(list==null || list.size()<1){
			throw new MyException("根据参数查找电影失败,参数信息:"+filmInfo);
		}
		return list;
	}

}
